<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.2.1/axios.js"></script>
</head>

<body>
    <button>添加学员</button>
    <button>获取学员</button>
    <button>删除学员</button>
    <ul></ul>
    <button>获取一言数据</button>
    <p></p>
    <script>
        // 获取ul和按钮
        let ul = document.querySelector('ul');
        let btns = document.querySelectorAll('button');
        let p = document.querySelector('p');

        // 1. 创建axios实例
        let studentsAxios = axios.create({
            baseURL: "http://localhost",
            method: "post"
        });

        // console.log(a1);

        //添加学员
        btns[0].onclick = async function() {
            let data = await studentsAxios({
                url: "/students",
                data: {
                    name: "赖安杰"
                }
            })

            console.log(data);
        }

        //获取学员
        btns[1].onclick = async function() {
            let data = await studentsAxios({
                url: "/students",
                method: "get",
            })

            console.log(data);
        }

        //删除学员
        btns[2].onclick = async function() {
            console.log("执行了");
            let data = await studentsAxios({
                url: "/students/14",
                method: "delete",
            })

            console.log(data);
        }


        // 2. 创建 axios实例
        let yiyanAxios = axios.create({
            baseURL: "https://v1.hitokoto.cn"
        })

        // 获取一言数据
        btns[3].onclick = async function() {
            let data = await yiyanAxios({
                params: {
                    c: 'i'
                }
            });

            console.log(data);
        }
    </script>
</body>

</html>